/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.usergui;

import Print.*;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import oracle.net.aso.d;
import org.math.plot.Plot2DPanel;
import stendcontrol.Experiment;
import stendcontrol.Func;
import stendcontrol.Param;

/**
 *
 * @author minaev
 */
public class ResultFrame extends javax.swing.JFrame {

    private Experiment exp;
    Plot2DPanel last_plot_panel;

    public ResultFrame(Experiment exp) {
        this.exp = exp;
        initComponents();

        this.setTitle("Результаты эксперимета: " + exp.getExpName());

        jl_nameexp.setText(exp.getExpName());

        if (exp.getDataResult().getCountFuncs() != 0) {
            addPlot();
            exp.getDataResult().getFunc(0).addToPlot(last_plot_panel);
            last_plot_panel.plotCanvas.addBaseLabel(
                    exp.getDataResult().getFunc(0).getLabelY(), Color.black,0,1.1);
            last_plot_panel.plotCanvas.addBaseLabel(
                    exp.getDataResult().getFunc(0).getLabelX(), Color.black,1.15,0);
        }

        for (int i = 1; i < exp.getDataResult().getCountFuncs(); i++) {
            if (exp.isNextGraph(i)) {
                addPlot();
                last_plot_panel.plotCanvas.addBaseLabel(
                    exp.getDataResult().getFunc(i).getLabelY(), Color.black,0,1.1);
                last_plot_panel.plotCanvas.addBaseLabel(
                    exp.getDataResult().getFunc(i).getLabelX(), Color.black,1.15,0);
            }
            exp.getDataResult().getFunc(i).addToPlot(last_plot_panel);
        }

        for (int i = 0; i < exp.getDataResult().getCountParams(); i++) {
            addParam(exp.getDataResult().getParam(i).getName(),
                    Double.toString(exp.getDataResult().getParam(i)
                    .getValue()));
        }

        jp_graphs.repaint();
        jp_params.repaint();
    }

    private void addPlot() {
        Plot2DPanel res = new Plot2DPanel();

        getJp_graphs().add(res);
        last_plot_panel = res;
        res.setLegendOrientation("EAST");
    }

    private void addParam(String param_name, String param_value) {
        JLabel lbl = new JLabel(param_name);
        JTextField tf = new JTextField(param_value);
        tf.setEditable(false);
        getJp_params().add(lbl);
        getJp_params().add(tf);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jl_nameexp = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jp_graphs = new javax.swing.JPanel();
        jp_params = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jl_nameexp.setFont(new java.awt.Font("Ubuntu", 0, 18)); // NOI18N
        jl_nameexp.setText("Name_exp");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jl_nameexp, javax.swing.GroupLayout.DEFAULT_SIZE, 593, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jl_nameexp)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jp_graphs.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        jp_graphs.setLayout(new java.awt.GridLayout(0, 1));

        jp_params.setBorder(javax.swing.BorderFactory.createTitledBorder("Результаты"));
        jp_params.setLayout(new java.awt.GridLayout(0, 2));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jp_graphs, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jp_params, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jp_params, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jp_graphs, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ResultFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ResultFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ResultFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ResultFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                int cnt = 2000;
                double[] x = new double[cnt];
                double[] y0 = new double[cnt];
                double[] y1 = new double[cnt];
                double[] y2 = new double[cnt];
                double[] y3 = new double[cnt];

                for (int i = 0; i < cnt; i++) {
                    x[i] = i;
                    y0[i] = Math.sin(i / 250.0) * 200;
                    y1[i] = Math.sin(i / 200.0) * 180;
                    y2[i] = Math.sin(i / 150.0) * 160;
                    y3[i] = Math.sin(i / 100.0) * 140;
                }
                Func fnc0 = new Func("test1", Func.F_OTHER, y0, x);
                Func fnc1 = new Func("test2", Func.F_OTHER, y1, x);
                Func fnc2 = new Func("test3", Func.F_OTHER, y2, x);
                Func fnc3 = new Func("test4", Func.F_OTHER, y3, x);
                Func fnc4 = new Func("test1", Func.F_OTHER, y0, x);
                Func fnc5 = new Func("test2", Func.F_OTHER, y1, x);
                Func fnc6 = new Func("test3", Func.F_OTHER, y2, x);
                Func fnc7 = new Func("test4", Func.F_OTHER, y3, x);
                Func fnc8 = new Func("test1", Func.F_OTHER, y1, x);
                Func fnc9 = new Func("test2", Func.F_OTHER, y0, x);
                Func fnc10 = new Func("test3", Func.F_OTHER, y2, x);
                Func fnc11 = new Func("test4", Func.F_OTHER, y3, x);

                Experiment test_exp = new Experiment("test_exp_result");

                test_exp.addResultFunc(fnc0, false);
                test_exp.addResultFunc(fnc1, false);
                test_exp.addResultFunc(fnc2, true);
                test_exp.addResultFunc(fnc3, false);

                test_exp.addResultFunc(fnc4, false);
                test_exp.addResultFunc(fnc5, false);
                test_exp.addResultFunc(fnc6, true);
                test_exp.addResultFunc(fnc7, false);

                test_exp.addResultFunc(fnc8, true);
                test_exp.addResultFunc(fnc9, false);
                test_exp.addResultFunc(fnc10, false);
                test_exp.addResultFunc(fnc11, false);

                test_exp.addResultParam(new Param("Параметр 1", Param.P_OTHER, 3));
                test_exp.addResultParam(new Param("Параметр 2", Param.P_OTHER, 300));
                test_exp.addResultParam(new Param("Параметр 3", Param.P_OTHER, 400));
                test_exp.addResultParam(new Param("Параметр 4", Param.P_OTHER, 3.1415));
                test_exp.addResultParam(new Param("Параметр 5", Param.P_OTHER, 234));
                test_exp.addResultParam(new Param("Параметр 6", Param.P_OTHER, 362.0));
                test_exp.addResultParam(new Param("Параметр 7", Param.P_OTHER, 4040));
                test_exp.addResultParam(new Param("Параметр 8", Param.P_OTHER, 3.1415));

                new ResultFrame(test_exp).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel jl_nameexp;
    private javax.swing.JPanel jp_graphs;
    private javax.swing.JPanel jp_params;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the jp_graphs
     */
    public javax.swing.JPanel getJp_graphs() {
        return jp_graphs;
    }

    /**
     * @return the jp_params
     */
    public javax.swing.JPanel getJp_params() {
        return jp_params;
    }

    /**
     * @return the exp
     */
    public Experiment getExp() {
        return exp;
    }

    /**
     * @param jp_graphs the jp_graphs to set
     */
    public void setJp_graphs(javax.swing.JPanel jp_graphs) {
        this.jp_graphs = jp_graphs;
    }
}
